package cn.edu.jju.stepcounter2.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import cn.edu.jju.stepcounter2.R;

import static android.util.Log.i;

/**
 * Created by roor on 2016/12/12.
 */

public class CircleView extends View {

    private RectF rectF;
    private Paint arcPaint;
    private float progress;
    private AddAnimation animation;
    private Paint textPaint;
    private Paint tipsPaint;
    private int steps = 0;
    private int totalStep;
    private int expectStep = 0;
    private int stepColor = R.color.primary_text;
    private int arcColor = R.color.primary;
    private int tipsColor = R.color.secondary_text;
    private Paint buttomPaint;


    private int arcWidth = 40;

    public CircleView(Context context) {
        super(context);
        init();

    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }


    @Override
    protected void onDraw(Canvas canvas) {
        arcPaint.setColor(getResources().getColor(arcColor));
        textPaint.setColor(getResources().getColor(stepColor));
        tipsPaint.setColor(getResources().getColor(tipsColor));
        arcPaint.setStrokeWidth(arcWidth);
        rectF.top = arcWidth;
        rectF.left = arcWidth;
        int size = Math.min(getHeight(), getWidth()) - arcWidth;
        rectF.bottom = size;
        rectF.right = size;
        buttomPaint.setStrokeWidth(arcWidth);
        canvas.drawArc(rectF, -90, progress, false, arcPaint);
        canvas.drawArc(rectF, -90, 360, false, buttomPaint);
        textPaint.setTextSize(90);
        tipsPaint.setTextSize(40);
        canvas.drawText("步數", getMeasuredWidth() / 2, (getMeasuredWidth() / 2) - (textPaint.descent() - textPaint.ascent()), tipsPaint);
        //i("@@@@", "onDraw" + textPaint.descent() + "-----" + textPaint.ascent());
        canvas.drawText(steps + "", getMeasuredWidth() / 2, getMeasuredWidth() / 2, textPaint);
        canvas.drawText("目標" + expectStep + "步", getMeasuredWidth() / 2, getMeasuredWidth() / 2 - textPaint.ascent() + textPaint.descent(), tipsPaint);
        //run();

        super.onDraw(canvas);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        i("@@@@", "width" + widthMeasureSpec + "\nheight----" + heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int width;
        int height;
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = 200;


        }
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = 200;

        }
        int size = Math.min(width, height);
        setMeasuredDimension(size, size);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    public void startCustomAnimation() {
        this.startAnimation(animation);
        //animation.startNow();

        i("aaa", "startCustomAnimation" + progress + "----" + steps+"----"+totalStep+"----"+expectStep+"----"+animation);
    }

    public class AddAnimation extends Animation {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {

            super.applyTransformation(interpolatedTime, t);

            progress = interpolatedTime * totalStep / expectStep * 360;
            i("@@@@", "applyTransformation" + totalStep + expectStep);
            steps = (int) (totalStep * interpolatedTime);
            postInvalidate();


        }
    }


    private void init() {
        arcPaint = new Paint();
        arcPaint.setStyle(Paint.Style.STROKE);
        arcPaint.setAntiAlias(true);
        rectF = new RectF();
        buttomPaint = new Paint();
        buttomPaint.setStyle(Paint.Style.STROKE);

        buttomPaint.setColor(getResources().getColor(R.color.cardview_shadow_start_color));


        textPaint = new Paint();

        textPaint.setTextAlign(Paint.Align.CENTER);

        tipsPaint = new Paint();
        tipsPaint.setTextAlign(Paint.Align.CENTER);
        animation = new AddAnimation();
        animation.setDuration(2000);


    }

    public void setArcWidth(int arcWidth) {
        this.arcWidth = arcWidth;

    }

    public void setTotalStep(int totalStep) {
        this.totalStep = totalStep;
        i("aaa", "setTotalStep" + totalStep);


    }

    public void setExpectStep(int expectStep) {
        this.expectStep = expectStep;
        i("@@@@", "setExpectStep" + expectStep);


    }

    public int getSteps() {
        return totalStep;
    }

    public void setSteps(int steps) {
        this.steps = steps;

    }

    public void setStepColor(int stepColor) {
        this.stepColor = stepColor;
    }

    public void setArcColor(int arcColor) {
        this.arcColor = arcColor;
    }

    public void setTipsColor(int tipsColor) {
        this.tipsColor = tipsColor;
    }

    public void setProgress(float progress) {
        this.progress = progress;
        i("aaa", "setProgress" + progress);

    }

}
